package com.nbsaas.boot.pay.ext.resource;

import com.nbsaas.boot.jpa.data.entity.User;
import com.nbsaas.boot.pay.api.apis.PaymentApi;
import com.nbsaas.boot.pay.api.domain.request.PaymentRequest;
import com.nbsaas.boot.pay.data.entity.Payment;
import com.nbsaas.boot.pay.ext.apis.PaymentExtApi;
import com.nbsaas.boot.pay.ext.domain.enums.PayState;
import com.nbsaas.boot.pay.ext.domain.enums.PayType;
import com.nbsaas.boot.pay.ext.domain.request.OrderPayRequest;
import com.nbsaas.boot.pay.ext.domain.request.PayBack;
import com.nbsaas.boot.pay.ext.domain.request.PayInfo;
import com.nbsaas.boot.pay.ext.domain.response.OrderPayResponse;
import com.nbsaas.boot.pay.ext.domain.simple.WeiXinPaySimple;
import com.nbsaas.boot.pay.ext.plugin.WeiAppPaymentPlugin;
import com.nbsaas.boot.pay.utils.DateUtils;
import com.nbsaas.boot.pay.utils.MapUtils;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.shop.ext.apis.ShopConfigExtApi;
import com.nbsaas.boot.shop.ext.domain.config.WeChatPay;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

import static com.nbsaas.boot.pay.ext.plugin.WeiAppPaymentPlugin.MD5Encode;
import static com.nbsaas.boot.pay.ext.plugin.WeiAppPaymentPlugin.getNonceStr;


@Service
public class PaymentExtResource implements PaymentExtApi {

    @Resource
    private PaymentApi paymentApi;

    @Resource
    private ShopConfigExtApi shopConfigExtApi;

    @Override
    public ResponseObject<OrderPayResponse> pay(OrderPayRequest request) {
        ResponseObject<OrderPayResponse> result = new ResponseObject<>();
        PayInfo payinfo = new PayInfo();
        if (!StringUtils.hasText(request.getOpenId())) {
            result.setCode(503);
            result.setMsg("没有通过小程序登陆");
            return result;
        }
        if (request.getShop() == null) {
            result.setCode(503);
            result.setMsg("商店不存在");
            return result;
        }

        ResponseObject<WeChatPay> temp = shopConfigExtApi.info(request.getShop(), WeChatPay.class);
        if (temp.getCode() != 200) {
            result.setCode(504);
            result.setMsg("支付方式不存在");
            return result;
        }
        WeChatPay pay = temp.getData();
        if (pay.getPayType()==null){
            pay.setPayType(1);
        }

        payinfo.setOpenid(request.getOpenId());
        payinfo.setMoney(request.getMoney());
        payinfo.setPayType(request.getPayType());
        payinfo.setBody("充值");
        if(pay.getPayType()==2){
            //1：直连模式 2：服务商模式
            payinfo.setPayConfigType(PayType.platform);
            payinfo.setPlatformId(pay.getPlatformId());
            payinfo.setPlatformMchId(pay.getServiceMchId());
        }

        payinfo.setSubAppId(pay.getAppId());
        payinfo.setSubMchId(pay.getMchId());

        payinfo.setKey(pay.getMchKey());
        payinfo.setNotifyUrl(pay.getNotifyUrl());


        WeiAppPaymentPlugin paymentPlugin = new WeiAppPaymentPlugin();
        ResponseObject<PayBack> payBack = paymentPlugin.payConfig(payinfo);
        if (payBack.getCode() != 200) {
            result.setCode(payBack.getCode());
            result.setMsg(payBack.getMsg());
            return result;
        }
        PayBack back = payBack.getData();
        String appId = pay.getAppId();
        String key = pay.getMchKey();

        WeiXinPaySimple paySimple = new WeiXinPaySimple();
        paySimple.setAppid(appId);
        paySimple.setNoncestr(getNonceStr());
        paySimple.setPackageInfo("prepay_id=" + back.getNo());
        paySimple.setTimestamp("" + DateUtils.second());
        paySimple.setPrepayid(back.getNo());
        paySimple.setSignType("MD5");

        Map<String, String> map = MapUtils.getSortMap();
        map.put("appId", paySimple.getAppid());
        map.put("package", paySimple.getPackageInfo());
        map.put("nonceStr", paySimple.getNoncestr());
        map.put("timeStamp", paySimple.getTimestamp());
        map.put("signType", paySimple.getSignType());

        String p = MapUtils.params(map);
        p = p + "&key=" + key;
        System.out.println(p);
        String sign = MD5Encode(p);
        paySimple.setSign(sign);


        String no = back.getNo();

        if (request.getPayHandler() == null) {
            request.setPayHandler("commonHandler");
        }
        PaymentRequest payment = new PaymentRequest();
        payment.setPayNo(back.getOrderNo());
        payment.setAmount(request.getMoney());
        payment.setUser(request.getUser());
        payment.setPayState(PayState.wait);
        payment.setHandle(request.getPayHandler());
        payment.setBussId(request.getDataId());
        paymentApi.create(payment);


        OrderPayResponse data = new OrderPayResponse();
        result.setData(data);
        data.setDataId(request.getDataId());
        data.setNo(no);
        data.setPay(paySimple);
        return result;
    }
}
